﻿/*
创建人：zhunan
日期：2023.1.16
描述：批次后缀维护js
*/
var page = {
  pagetotal: 0,
  pageSize: 12,
  pageNumber: 1,
  mainUrl: getRootPath() + "/Ashx/ContainerSuffixMaint.ashx", //本页面后台路径
  currentUserInfo: {},
  //初始化页面
  init: function () {
    page.bind();
    page.initData(); //初始化页面数据
  },
  // 页面事件绑定
  bind: function () {
    // 批次信息
    $("#tbContainer").datagrid({
      columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'containerid', title: '批次id', hidden: true },
        { field: 'mfgordername', title: '订单号', width: 100 },
        { field: 'productname', title: '件号', width: 100 },
        {
          field: 'containernamenew', title: '批次号', width: 360,
          formatter: function (value) {
            if (value.indexOf(';') > -1) {
              return value.replace(';', '<br/>');
            } else {
              return value;
            }
          }
        },
        { field: 'customobjectitemsname', title: '后缀标识', width: 100 },
        { field: 'customobjectitemsdesc', title: '后缀说明', width: 100 },
        { field: 'customobjectitemsnotes', title: '备注', width: 100 }
      ]],
      onLoadSuccess: function (data) {
        $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
          total: page.pagetotal,
          pageSize: page.pageSize,
          pageNumber: page.pageNumber
        });
        customizedEasyuiPagination('pp'); //定制化分页
      },
      //行点击事件
      onClickRow: function (rowIndex, rowData) { }
    });

    //批次表格分页
    $('#pp').pagination({
      onSelectPage: function (pageNumber, pageSize) {
        customizedEasyuiPagination('pp'); //定制化分页
        page.pageSize = pageSize;
        page.pageNumber = pageNumber;
        page.search();
      }
    });

    //绑定分页go按钮
    $(function () {
      $('#pageGo').on("click", function () {
        var pageNumber = $(this).parents('tr').find('.pagination-num').val();
        page.pageNumber = pageNumber;
        page.search();
      });
    });

    // 查询
    $("#btnSearch").on("click", function () {
      page.pageNumber = 1;
      page.search();
    });

    // 重置按钮
    $('#btnReset').on("click", function () {
      page.resetData();
    });

    // 添加后缀
    $("#btnSave").on("click", function () {
      var datas = $("#tbContainer").datagrid('getChecked');
      if (datas.length === 0) {
        displayMessage("请勾选需要添加后缀的批次！", false);
        return;
      }
      if (datas.length >= 2) {
        displayMessage("一次只能勾选1条！", false);
        return;
      }

      var containerid = datas[0].containerid;
      var containername = datas[0].containername;
      var mfgordername = datas[0].mfgordername;
      $("#txtDialogContainerid").textbox('setValue', containerid);
      $("#txtDialogMfgordername").textbox('setValue', mfgordername);
      $("#txtDialogContainername").textbox('setValue', containername);
      $('#dialog').window('open');
    });

    // 删除后缀
    $("#btnDel").on("click", function () {
      var datas = $("#tbContainer").datagrid('getChecked');
      if (datas.length === 0) {
        displayMessage("请勾选需要删除后缀的批次！", false);
        return;
      }
      $.messager.confirm('删除前确认', '你确定要删除吗?', function (r) {
        // 确认删除
        if (r) {
          page.removeData(datas);
        }
      });
    });

    // 对话框 保存 按钮
    $("#btnDialogSave").on('click', function () {
      var customobjectitemsid = $('#ddlDialogCumtomObjectItemsName').combobox('getValue');
      var containerid = $('#txtDialogContainerid').textbox('getValue');
      if (!customobjectitemsid) {
        $("#dialogMessage").text('消息：请选择后缀标识！').css('color', 'red');
        return;
      }
      var data = {
        action: "saveData",
        containerid: containerid,
        customobjectitemsid: customobjectitemsid
      }
      page.saveData(data);
    });

    // 对话框 关闭 按钮
    $("#btnDialogClose").on('click', function () {
      $('#dialog').window('close');
      $("#txtDialogContainerid").textbox('clear');
      $("#txtDialogMfgordername").textbox('clear');
      $("#txtDialogContainername").textbox('clear');
      $("#txtDialogCustomObjectItemsDesc").textbox('clear');
      $("#txtDialogCustomObjectItemsNotes").textbox('clear');
      $('#ddlDialogCumtomObjectItemsName').combobox('select', '');
      page.search();
    });
  },
  //初始化页面
  initData: function () {
    page.getCustomObjectItems();
    page.getCurrentUserInfo();
  },
  // 获取当前登录用户
  getCurrentUserInfo: function () {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=GetCurrentUserInfo",
      data: { action: "GetCurrentUserInfo" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.ReturnData) {
          page.currentUserInfo = res.ReturnData;
          var data = [{ factoryid: page.currentUserInfo.FactoryID, factoryname: page.currentUserInfo.Factory }];
          $("#ddlFactory").combobox({
            valueField: 'factoryid',
            textField: 'factoryname',
            panelHeight: 200,
            data: data
          });
          $("#ddlFactory").combobox('select', page.currentUserInfo.FactoryID);
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 查询自定义对象
  getCustomObjectItems: function () {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?action=getCustomObjectItems",
      data: { action: "getCustomObjectItems" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          res.DBTable.unshift({ customobjectid: '', customobjectname: '' });
          $("#ddlDialogCumtomObjectItemsName").combobox({
            valueField: 'customobjectitemsid',
            textField: 'customobjectitemsname',
            panelHeight: 200,
            data: res.DBTable,
            onSelect: function (r) {
              if (r.customobjectitemsid) {
                $('#txtDialogCustomObjectItemsDesc').textbox('setValue', r.customobjectitemsdesc);
                $('#txtDialogCustomObjectItemsNotes').textbox('setValue', r.customobjectitemsnotes);
              }
            }
          });
        }
      }
    });
  },
  // 查询
  search: function (pageNumber) {
    $('#tbContainer').datagrid('loadData', []);
    var formData = {
      action: "getData",
      pageSize: page.pageSize,
      pageNumber: pageNumber || page.pageNumber,
      factoryid: $("#ddlFactory").combobox('getValue'),
      customobjectitemsname: $("#txtCustomObjectItemsName").textbox('getValue'),
      mfgordername: $('#txtMfgordername').textbox('getValue'),
      productname: $('#txtProductname').textbox('getValue'),
      containername: $('#txtContainername').textbox('getValue')
    };

    $.ajax({
      type: "Post",
      url: page.mainUrl + "?actions=getData",
      data: formData,
      cashe: false,
      async: true,
      beforeSend: function () {
        $('#tbContainer').datagrid('loading');
        $(this).attr("disabled", "disabled"); //禁用按钮
      },
      success: function (data) {
        $('#tbContainer').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        var result = JSON.parse(data);
        if (result.Result) {
          //给分页对象赋值
          page.pagetotal = result.RowCount;
          $('#tbContainer').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
        } else {
          displayMessage(result.Message, false);
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $('#tbContainer').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        alert(XMLHttpRequest.responseText);
      }
    });
  },
  // 添加
  saveData: function (data) {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?action=saveData",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('保存成功！', true);
          $("#txtDialogContainerid").textbox('clear');
          $("#txtDialogMfgordername").textbox('clear');
          $("#txtDialogContainername").textbox('clear');
          $("#txtDialogCustomObjectItemsDesc").textbox('clear');
          $("#txtDialogCustomObjectItemsNotes").textbox('clear');
          $('#ddlDialogCumtomObjectItemsName').combobox('select', '');
          $('#dialog').window('close');
          page.search();
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  },
  // 移除数据
  removeData: function (selectdata) {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?action=removeData",
      data: {
        action: "removeData",
        willBeDeleteData: JSON.stringify(selectdata)
      },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('批次后缀移除成功！', true);
          page.search();
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  },
  // 清除方法，重要！！！
  reset: function (iscondition, isresult) {
    clearMessage();
    if (isresult) {
      clearEasyuiDataGrid("tbContainer", 1);
    }
  },
  // 重置
  resetData: function () {
    $("#txtCustomObjectItemsName").textbox('clear');
    $('#txtMfgordername').textbox('clear');
    $('#txtProductname').textbox('clear');
    $('#txtContainername').textbox('clear');
    $("#tbContainer").datagrid('loadData', { total: 0, rows: [] });
    page.pageNumber = 1;
    clearMessage();
  }
};
page.init();